home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / gnu / gnulib / ohlutil / modechan.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-08-24  |  9.9 KB  |  350 lines

  1. /* modechange.c -- file mode manipulation
  2.    Copyright (C) 1989, 1990 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 1, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /*
  19.  * MS-DOS port (c) 1990 by Thorsten Ohl, td12@ddagsi3.bitnet
  20.  *
  21.  * To this port, the same copying conditions apply as to the
  22.  * original release.
  23.  *
  24.  * IMPORTANT:
  25.  * This file is not identical to the original GNU release!
  26.  * You should have received this code as patch to the official
  27.  * GNU release.
  28.  *
  29.  * MORE IMPORTANT:
  30.  * This port comes with ABSOLUTELY NO WARRANTY.
  31.  *
  32.  * $Header: e:/gnu/fileutil/RCS/modechan.c'v 1.3.0.2 90/06/29 00:47:03 tho Stable $
  33.  */
  34.  
  35. /* Written by David MacKenzie <djm@ai.mit.edu> */
  36.  
  37. /* The ASCII mode string is compiled into a linked list of `struct
  38.    modechange', which can then be applied to each file to be changed.
  39.    We do this instead of re-parsing the ASCII string for each file
  40.    because the compiled form requires less computation to use; when
  41.    changing the mode of many files, this probably results in a
  42.    performance gain. */
  43.  
  44. #include <sys/types.h>
  45. #include <sys/stat.h>
  46. #include "modechange.h"
  47.  
  48. #ifdef MSDOS
  49. #include <stdio.h>
  50. #include <io.h>
  51. static int oatoi (char *s);
  52. #endif /* MSDOS */
  53.  
  54. #ifdef STDC_HEADERS
  55. #include <stdlib.h>
  56. #else
  57. char *malloc ();
  58.  
  59. #ifndef NULL
  60. #define NULL 0
  61. #endif
  62. #endif
  63.  
  64. /* Return newly allocated memory to hold one element of type TYPE. */
  65. #define talloc(type) ((type *) malloc (sizeof (type)))
  66.  
  67. #define isodigit(c) ((c) >= '0' && (c) <= '7')
  68.  
  69. static int oatoi ();
  70.  
  71. /* Return a linked list of file mode change operations created from
  72.    MODE_STRING, an ASCII string that contains either an octal number
  73.    specifying an absolute mode, or symbolic mode change operations with
  74.    the form:
  75.    [ugoa...][[+-=][rwxXstugo...]...][,...]
  76.    MASKED_OPS is a bitmask indicating which symbolic mode operators (=+-)
  77.    should not affect bits set in the umask when no users are given.
  78.    Operators not selected in MASKED_OPS ignore the umask.
  79.  
  80.    Return MODE_INVALID if `mode_string' does not contain a valid
  81.    representation of file mode change operations;
  82.    return MODE_MEMORY_EXHAUSTED if there is insufficient memory. */
  83.  
  84. struct mode_change *
  85. mode_compile (mode_string, masked_ops)
  86.      register char *mode_string;
  87.      unsigned masked_ops;
  88. {
  89.   struct mode_change *head;    /* First element of the linked list. */
  90.   struct mode_change *change;    /* An element of the linked list. */
  91.   int i;            /* General purpose temporary. */
  92.   int umask_value;        /* The umask value (surprise). */
  93.   unsigned short affected_bits;    /* Which bits in the mode are operated on. */
  94.   unsigned short affected_masked; /* `affected_bits' modified by umask. */
  95.   unsigned ops_to_mask;        /* Operators to actually use umask on. */
  96.  
  97.   i = oatoi (mode_string);
  98.   if (i >= 0)
  99.     {
  100.       if (i > 07777)
  101.     return MODE_INVALID;
  102.       head = talloc (struct mode_change);
  103.       if (head == NULL)
  104.     return MODE_MEMORY_EXHAUSTED;
  105.       head->next = NULL;
  106.       head->op = '=';
  107.       head->flags = 0;
  108.       head->value = i;
  109.       head->affected = 07777;    /* Affect all permissions. */
  110.       return head;
  111.     }
  112.  
  113.   umask_value = umask (0);
  114.   umask (umask_value);        /* Restore the old value. */
  115.  
  116.   head = NULL;
  117.   --mode_string;
  118.  
  119.   /* One loop iteration for each "ugoa...=+-rwxXstugo...[=+-rwxXstugo...]". */
  120.   do
  121.     {
  122.       affected_bits = 0;
  123.       ops_to_mask = 0;
  124.       /* Turn on all the bits in `affected_bits' for each group given. */
  125.       for (++mode_string;; ++mode_string)
  126.     switch (*mode_string)
  127.       {
  128.       case 'u':
  129.         affected_bits |= 04700;
  130.         break;
  131.       case 'g':
  132.         affected_bits |= 02070;
  133.         break;
  134.       case 'o':
  135.         affected_bits |= 01007;
  136.         break;
  137.       case 'a':
  138.         affected_bits |= 07777;
  139.         break;
  140.       default:
  141.         goto no_more_affected;
  142.       }
  143.  
  144.     no_more_affected:
  145.       /* If none specified, affect all bits, except perhaps those
  146.      set in the umask. */
  147.       if (affected_bits == 0)
  148.     {
  149.       affected_bits = 07777;
  150.       ops_to_mask = masked_ops;
  151.     }
  152.  
  153.       while (*mode_string == '=' || *mode_string == '+' || *mode_string == '-')
  154.     {
  155.       /* Add the element to the tail of the list, so the operations
  156.          are performed in the correct order. */
  157.       if (head == NULL)
  158.         {
  159.           head = talloc (struct mode_change);
  160.           if (head == NULL)
  161.         return MODE_MEMORY_EXHAUSTED;
  162.           change = head;
  163.         }
  164.       else
  165.         {
  166.           change->next = talloc (struct mode_change);
  167.           if (change->next == NULL)
  168.         {
  169.           mode_free (change);
  170.           return MODE_MEMORY_EXHAUSTED;
  171.         }
  172.           change = change->next;
  173.         }
  174.  
  175.       change->next = NULL;
  176.       change->op = *mode_string;    /* One of "=+-". */
  177.       affected_masked = affected_bits;
  178.       if (ops_to_mask & (*mode_string == '=' ? MODE_MASK_EQUALS
  179.                  : *mode_string == '+' ? MODE_MASK_PLUS
  180.                  : MODE_MASK_MINUS))
  181.         affected_masked &= ~umask_value;
  182.       change->affected = affected_masked;
  183.       change->value = 0;
  184.       change->flags = 0;
  185.  
  186.       /* Set `value' according to the bits set in `affected_masked'. */
  187.       for (++mode_string;; ++mode_string)
  188.         switch (*mode_string)
  189.           {
  190.           case 'r':
  191.         change->value |= 00444 & affected_masked;
  192.         break;
  193.           case 'w':
  194.         change->value |= 00222 & affected_masked;
  195.         break;
  196.           case 'X':
  197.         change->flags |= MODE_X_IF_ANY_X;
  198.         /* Fall through. */
  199.           case 'x':
  200.         change->value |= 00111 & affected_masked;
  201.         break;
  202.           case 's':
  203.         /* Set the setuid/gid bits if `u' or `g' is selected. */
  204.         change->value |= 06000 & affected_masked;
  205.         break;
  206.           case 't':
  207.         /* Set the "save text image" bit if `o' is selected. */
  208.         change->value |= 01000 & affected_masked;
  209.         break;
  210.           case 'u':
  211.         /* Set the affected bits to the value of the `u' bits
  212.            on the same file.  */
  213.         if (change->value)
  214.           goto invalid;
  215.         change->value = 00700;
  216.         change->flags |= MODE_COPY_EXISTING;
  217.         break;
  218.           case 'g':
  219.         /* Set the affected bits to the value of the `g' bits
  220.            on the same file.  */
  221.         if (change->value)
  222.           goto invalid;
  223.         change->value = 00070;
  224.         change->flags |= MODE_COPY_EXISTING;
  225.         break;
  226.           case 'o':
  227.         /* Set the affected bits to the value of the `o' bits
  228.            on the same file.  */
  229.         if (change->value)
  230.           goto invalid;
  231.         change->value = 00007;
  232.         change->flags |= MODE_COPY_EXISTING;
  233.         break;
  234.           default:
  235.         goto no_more_values;
  236.           }
  237.     no_more_values:;
  238.     }
  239.   } while (*mode_string == ',');
  240.   if (*mode_string == 0)
  241.     return head;
  242. invalid:
  243.   mode_free (head);
  244.   return MODE_INVALID;
  245. }
  246.  
  247. /* Return file mode OLDMODE, adjusted as indicated by the list of change
  248.    operations CHANGES.  If OLDMODE has the S_IFDIR bit set, the type `X'
  249.    change affects it even if no execute bits were set in OLDMODE.
  250.    The returned value has the S_IFMT bits cleared. */
  251.  
  252. unsigned short
  253. mode_adjust (oldmode, changes)
  254.      unsigned oldmode;
  255.      register struct mode_change *changes;
  256. {
  257.   unsigned short newmode;    /* The adjusted mode and one operand. */
  258.   unsigned short value;        /* The other operand. */
  259.  
  260.   newmode = oldmode & 07777;
  261.  
  262.   for (; changes; changes = changes->next)
  263.     {
  264.       if (changes->flags & MODE_COPY_EXISTING)
  265.     {
  266.       /* Isolate in `value' the bits in `newmode' to copy, given in
  267.          the mask `changes->value'. */
  268.       value = newmode & changes->value;
  269.  
  270.       if (changes->value & 00700)
  271.         /* Copy `u' permissions onto `g' and `o'. */
  272.         value |= (value >> 3) | (value >> 6);
  273.       else if (changes->value & 00070)
  274.         /* Copy `g' permissions onto `u' and `o'. */
  275.         value |= (value << 3) | (value >> 3);
  276.       else
  277.         /* Copy `o' permissions onto `u' and `g'. */
  278.         value |= (value << 3) | (value << 6);
  279.  
  280.       /* In order to change only `u', `g', or `o' permissions,
  281.          or some combination thereof, clear unselected bits.
  282.          This can not be done in mode_compile because the value
  283.          to which the `changes->affected' mask is applied depends
  284.          on the old mode of each file. */
  285.       value &= changes->affected;
  286.     }
  287.       else
  288.     {
  289.       value = changes->value;
  290.       /* If `X', do not affect the execute bits if the file is not a
  291.          directory and no execute bits are already set. */
  292.       if ((changes->flags & MODE_X_IF_ANY_X)
  293.           && (oldmode & S_IFMT) != S_IFDIR
  294.           && (newmode & 00111) == 0)
  295.         value &= ~00111;    /* Clear the execute bits. */
  296.     }
  297.  
  298.       switch (changes->op)
  299.     {
  300.     case '=':
  301.       /* Preserve the previous values in `newmode' of bits that are
  302.          not affected by this change operation. */
  303.       newmode = (newmode & ~changes->affected) | value;
  304.       break;
  305.     case '+':
  306.       newmode |= value;
  307.       break;
  308.     case '-':
  309.       newmode &= ~value;
  310.       break;
  311.     }
  312.     }
  313.   return newmode;
  314. }
  315.  
  316. /* Free the memory used by the list of file mode change operations
  317.    CHANGES. */
  318.  
  319. void
  320. mode_free (changes)
  321.      register struct mode_change *changes;
  322. {
  323.   register struct mode_change *next;
  324.  
  325.   while (changes)
  326.     {
  327.       next = changes->next;
  328.       free (changes);
  329.       changes = next;
  330.     }
  331. }
  332.  
  333. /* Return a positive integer containing the value of the ASCII
  334.    octal number S.  If S is not an octal number, return -1.  */
  335.  
  336. static int
  337. oatoi (s)
  338.      char *s;
  339. {
  340.   register int i;
  341.  
  342.   if (*s == 0)
  343.     return -1;
  344.   for (i = 0; isodigit (*s); ++s)
  345.     i = i * 8 + *s - '0';
  346.   if (*s)
  347.     return -1;
  348.   return i;
  349. }
  350.